package com.typy.www.tpay.util;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.typy.www.tpay.R;

import java.io.File;
import java.util.concurrent.ExecutionException;

/**
 * Created by 13799 on 2018/6/24.
 */

public class GlideUtils {

    public static GlideUtils instance;
    private Context mContext;
    private Glide glide;
    public static int mScreenWidth;
    public static int mScreenHeight;
    private static ImageMemCache gImageMemCache;

    public GlideUtils(Context context) {
        mContext = context;
    }

    public static void init(Context context) {
        if (instance == null) {
            instance = new GlideUtils(context);
        }
        mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = context.getResources().getDisplayMetrics().heightPixels;
        final int memClass = ((ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        final int memCacheSize = 1024 * 1024 * memClass / 4;
        gImageMemCache = new ImageMemCache(memCacheSize);
    }

    public ImageMemCache getImageMemCache() {
        return gImageMemCache;
    }

    public static GlideUtils getInstance() {
        return instance;
    }
//    public Glide getGlide(){
//        if(glide==null){
//            glide=Glide.with(mContext);
//        }
//        return glide;
//    }

    public void setImage(File file, ImageView imageView) {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int imageWidth = (dm.widthPixels - 20) / 3;
        Glide.with(mContext).load(file).
                override(imageWidth, imageWidth).
                placeholder(R.drawable.imgloading).
                error(R.drawable.loaderr1).
                crossFade().into(imageView);
    }

    public void setImage(Context context, File file, ImageView imageView) {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int imageWidth = (dm.widthPixels - 20) / 3;
        Glide.with(context).load(file).
                override(imageWidth, imageWidth).
                placeholder(R.drawable.imgloading).
                error(R.drawable.loaderr1).
                crossFade().into(imageView);
    }

    public Bitmap getBitma(File file) {
        Bitmap bitmap = null;
        try {
            bitmap = Glide.with(mContext)
                    .load(file)
                    .asBitmap()
                    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public void setImage(File file, int imageWidth, ImageView imageView) {
        Glide.with(mContext).load(file).
                override(imageWidth, imageWidth).
                placeholder(R.drawable.imgloading).
                error(R.drawable.loaderr1).
                crossFade().into(imageView);
    }

    public static void setImage(Context context, String url, ImageView imageView) {
        Glide.with(context).load(new LoadImage(url)).placeholder(R.drawable.imgloading).into(imageView);
    }

}
